#pragma once
#include <iostream>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "Socket.hpp"

static const uint16_t defaultport = 8888;
static const int fd_num_max = (sizeof(fd_set) * 8);
int defaultfd = -1;

class SelectServer
{

public:
    SelectServer(uint16_t port = defaultport) : port_(port)
    {
        // 将数组全部置为-1
        for (int i = 0; i < fd_num_max; ++i)
        {
            fd_array[i] = defaultfd;
        }
    }
    bool Init()
    {
        listensock_.Socket();
        listensock_.Bind(port_);
        listensock_.Listen();
        std::cout << "socker listen..." << std::endl;
        return true;
    }
    void Accepter()
    {
        std::string clientip;
        uint16_t clientport = 0;
        int sockfd = listensock_.Accept(&clientip, &clientport);
        if (sockfd < 0)
            return;
        // 将这个sockfd添加到数组中
        int pos = 1;
        for (; pos < fd_num_max; ++pos)
        {
            if (fd_array[pos] != defaultfd)
            {
                continue;
            }
            else
            {
                break;
            }
        }
        if (pos == fd_num_max)
        {
            std::cout << "server is full close :" << sockfd << std::endl;
            close(sockfd);
        }
        else
        {
            fd_array[pos] = sockfd;
        }
    }
    void Recver(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "get a message:" << buffer << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "client quit,me too,close fd is:" << fd << std::endl;
            close(fd);
            fd_array[pos] = defaultfd;
        }
        else
        {
            close(fd);
            fd_array[pos] = defaultfd;
        }
    }
    void Dispatcher(fd_set &rfds)
    {
        // 遍历数组
        for (int i = 0; i < fd_num_max; ++i)
        {
            int fd = fd_array[i];
            if (fd == defaultfd)
            {
                continue;
            }
            //判断关注的文件有没有就绪的
            // 如果文件描述符在位图中
            if (FD_ISSET(fd, &rfds))
            {
                // 判断文件描述符
                if (fd == listensock_.Sockfd())
                {
                    std::cout << "get a new link!" << std::endl;
                    // 进行连接的管理
                    Accepter();
                }
                else
                {
                    // 处理的文件描述符和在数组中的位置
                    Recver(fd, i);
                }
            }
        }
    }
    void Start()
    {
        int listensock = listensock_.Sockfd();
        fd_array[0] = listensock;
        while (true)
        {
            // 将套接字添加到select中
            fd_set rfds;
            FD_ZERO(&rfds);
            // 每次都要遍历fd_array，找出最大的文件描述符
            int maxfd = fd_array[0];
            for (int i = 0; i < fd_num_max; ++i)
            {
                if (fd_array[i] == defaultfd)
                    continue;
                    //把所有需要关注的事件添加进位图
                FD_SET(fd_array[i], &rfds);
                if (fd_array[i] > maxfd)
                {
                    maxfd = fd_array[i];
                    std::cout << "maxfd is:" << maxfd << std::endl;
                }
            }

            struct timeval timeout = {5, 0};
            int n = select(maxfd + 1, &rfds, nullptr, nullptr, &timeout /*nullptr*/ /*&timeout*/);
            switch (n)
            {
            case 0:
                std::cout << "time out,timeout:" << timeout.tv_sec << "," << timeout.tv_usec << std::endl;
                break;
            case -1:
                std::cerr << "select error" << std::endl;
                break;
            default:
                // 有事件就绪了
                // std::cout << "get a new link!" << std::endl;
                Dispatcher(rfds);
                break;
            }
        }
    }
    ~SelectServer()
    {
        close(listensock_.Sockfd());
    }

private:
    Sock listensock_;
    uint16_t port_;
    int fd_array[fd_num_max];
};